﻿using System;
using Business.Models;
using Volo.Abp.Domain.Repositories;
using XCZ;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Business.StorageManagement.Dto;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;
using System.Linq;
using Yitter.IdGenerator;
using SixLabors.ImageSharp;

namespace Business.StorageManagement
{
    //[Authorize(BusinessPermissions.Storage.Default)]
    public class StorageAppService : BusinessWorkFlowAppService, IStorageAppService
    {
        private const string FormName = "Storage";
        private IRepository<Storage, Guid> _repository;
        private IRepository<StorageList, Guid> _repositorylist;

        public StorageAppService(IRepository<Storage, Guid> repository, IRepository<StorageList, Guid> repositorylist)
        {
            _repository = repository;
            _repositorylist=repositorylist;
        }


        #region 增删改查基础方法
        public async Task<StorageDto> CreateOrUpdate(StorageDto input)
        {
            Storage result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                input.Number = YitIdHelper.NextId().ToString();
                Storage storage = new Storage()
                {
                    Title = input.Title,
                    Source = input.Source,
                    Name = input.Name,
                    Applicants = input.Applicants,
                    AppTime = input.AppTime,
                    AppType = input.AppType,
                    Dilivery = input.Dilivery,
                    GongYingName = input.GongYingName,

                };

                foreach(var item in input.GoodList)
                {
                    StorageList storageList = new StorageList()
                    {
                        Ids = item.Ids,
                        StorageIds = input.Id,
                        ShoppName = item.ShoppName,
                        ShoppNumber = item.ShoppNumber,
                        Specification = item.Specification,
                        Unit = item.Unit,
                        UnitPrice = item.UnitPrice,
                        Quantity = item.Quantity,
                        TotalPrice = item.TotalPrice,
                        BatchNumber = item.BatchNumber,
                        ProduceDate = item.ProduceDate,
                        ValidDate = item.ValidDate,
                        ArrivalDate = item.ArrivalDate,
                        Packing = item.Packing,
                        Remarks = item.Remarks,
                    };
                    await _repositorylist.InsertAsync(storageList);
                };
                result = await _repository.InsertAsync(ObjectMapper.Map<StorageDto,Storage>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result  = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Storage, StorageDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }
        }

        public async Task<StorageDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            return ObjectMapper.Map<Storage, StorageDto>(data);
        }

        public async Task<PagedResultDto<StorageDto>> GetAll(GetStorageInputDto input)
        {
            var query = (from a in await _repository.GetQueryableAsync()
                         select new StorageDto
                         {
                             Id = a.Id,
                             Title = a.Title,
                             Number = a.Number,
                             Source = a.Source,
                             Name = a.Name,
                             Applicants = a.Applicants,
                             Dilivery = a.Dilivery,
                             GongYingName = a.GongYingName,
                             AppTime = a.AppTime,
                             AppType = a.AppType,
                             GoodList = (from b in _repositorylist.GetQueryableAsync().Result
                                         where a.Id == b.StorageIds
                                         select new CreateOrUpdateStorageDto
                                         {
                                             Ids = b.Ids,
                                             ShoppName = b.ShoppName,
                                             ShoppNumber = b.ShoppNumber,
                                             Specification = b.Specification,
                                             Unit = b.Unit,
                                             UnitPrice = b.UnitPrice,
                                             Quantity = b.Quantity,
                                             TotalPrice = b.TotalPrice,
                                             BatchNumber = b.BatchNumber,
                                             ProduceDate = b.ProduceDate,
                                             ValidDate = b.ValidDate,
                                             ArrivalDate = b.ArrivalDate,
                                             Packing = b.Packing,
                                             Remarks = b.Remarks,
                                             StorageIds= a.Id
                                         }).ToList()           
                         }).ToList();
            
            var totalCount = query.Count;
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();
            return new PagedResultDto<StorageDto>(totalCount, query);

        }

        #endregion
    }
}
